#include "graphics/draw.h"
#include "system/window.h"
#include <SDL/SDL_gfxPrimitives.h>
#include <math.h>

EsdlColor esdlCreateColor(Uint8 red, Uint8 green, Uint8 blue) {
    EsdlColor color = {red, green, blue};
    return color;
}

void esdlFillSprite(EsdlSprite* sprite, EsdlColor color) {
    SDL_Rect rect;
    rect.x = 0;
    rect.y = 0;
    rect.w = sprite->surface->w;
    rect.h = sprite->surface->h;
    SDL_FillRect(sprite->surface,
            &rect,
            SDL_MapRGB(sprite->surface->format, color.r, color.g, color.b));
}

void esdlDrawLine(EsdlSprite* sprite, unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2, EsdlColor color, unsigned int thickness) {
    thickLineRGBA(sprite->surface, x1, y1, x2, y2, thickness, color.r, color.g, color.b, 255);
}

void esdlDrawCircle(EsdlSprite* sprite, unsigned int x_centre, unsigned int y_centre, unsigned int radius, EsdlColor color) {
    circleRGBA(sprite->surface, x_centre, y_centre, radius, color.r, color.g, color.b, 255);
}

void esdlDrawFilledCircle(EsdlSprite* sprite, unsigned int x_centre, unsigned int y_centre, unsigned int radius, EsdlColor color) {
    filledCircleRGBA(sprite->surface, x_centre, y_centre, radius, color.r, color.g, color.b, 255);
}

void esdlDrawEllipse(EsdlSprite* sprite, unsigned int x_centre, unsigned int y_centre, unsigned int radius_x, unsigned int radius_y, EsdlColor color) {
    ellipseRGBA(sprite->surface, x_centre, y_centre, radius_x, radius_y, color.r, color.g, color.b, 255);
}

void esdlDrawFilledEllipse(EsdlSprite* sprite, unsigned int x_centre, unsigned int y_centre, unsigned int radius_x, unsigned int radius_y, EsdlColor color) {
    filledEllipseRGBA(sprite->surface, x_centre, y_centre, radius_x, radius_y, color.r, color.g, color.b, 255);
}

void esdlDrawFilledRectangle(EsdlSprite* sprite, unsigned int x, unsigned int y, unsigned int width, unsigned int height, EsdlColor color) {
    SDL_Rect rect;
    rect.x = x;
    rect.y = y;
    rect.w = width;
    rect.h = height;
    SDL_FillRect(sprite->surface, &rect, SDL_MapRGB(sprite->surface->format, color.r, color.g, color.b));
}

void esdlDrawRectangle(EsdlSprite* sprite, unsigned int x, unsigned int y, unsigned int width, unsigned int height, EsdlColor color, unsigned int thickness) {
    esdlDrawFilledRectangle(sprite, x - thickness / 2, y - thickness / 2, thickness, height + thickness, color);
    esdlDrawFilledRectangle(sprite, x + width - thickness / 2, y - thickness / 2, thickness, height + thickness, color);
    esdlDrawFilledRectangle(sprite, x - thickness / 2, y - thickness / 2, width + thickness, thickness, color);
    esdlDrawFilledRectangle(sprite, x - thickness / 2, y + height - thickness / 2, width + thickness, thickness, color);
}


// ATTENTION : penser à locker la surface avant de modifier un pixel ! (SDL_LockSurface())
void esdlSetPixel(EsdlSprite* sprite, unsigned int x, unsigned int y, EsdlColor color) {
    pixelRGBA (sprite->surface, x, y, color.r, color.g, color.b, 255);
}

EsdlColor esdlGetPixel(const EsdlSprite* sprite, unsigned int x, unsigned int y) {
    EsdlColor color;
    int bpp = sprite->surface->format->BytesPerPixel;

    Uint8 *p = (Uint8 *) sprite->surface->pixels + y * sprite->surface->pitch + x * bpp;

    if (SDL_BYTEORDER == SDL_BIG_ENDIAN) {
        color.r = p[0];
        color.g = p[1];
        color.b = p[2];
    } else {
        color.b = p[0];
        color.g = p[1];
        color.r = p[2];
    }

    return color;
}

